﻿using Business;
using Common;
using SqliteDataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Printing;
using System.IO;
using System.Text;

namespace MVP
{
    public class BukuKasUmumPresenter : BasePresenter<IBukuKasUmumView>
    {
        private IBukuKasUmumView _view;
        private BukuKasUmumBusiness _business
        {
            get { return (BukuKasUmumBusiness)Business; }
        }

        public BukuKasUmumPresenter(IBukuKasUmumView view)
            : base(view)
        {
            _view = view;
            Business = new BukuKasUmumBusiness();
            //view.Save += OnViewSave;
        }

        private void OnViewSave(object sender, EventArgs e)
        {
            //int rows = _business.Save();
            //_view.SendMessageInfo(String.Format("Update data berhasil ({0} baris).", rows));
        }

        protected override void OnViewInitialize(object sender, EventArgs e)
        {
            int rows = _business.LoadByBulanTahun(_view.Bulan, Convert.ToInt32(_view.Tahun));
            _view.LppbopDataSet = (DataSet)_business.LppbopSqlite;
            //_view.SendMessageInfo(_business.ConnectionString);
        }

        public void ExportToExcel(string filename, DataSet dataset)
        {
            SqliteDataManager.BukuKasUmumDataTable table = ((SqliteDataManager)dataset).BukuKasUmum;
            if (table.Count == 0)
            {
                _view.SendMessageInfo("No data to export.");
            }
            _business.ExportToExcel(filename, table);
        }

        public DataTable GetUraianPenggunaan()
        {
            SqliteDataManager.UraianPenggunaanDataTable dt = _business.GetUraianPenggunaan();
            return dt;
        }

        public void Print(DataSet dataset, PrinterSettings printerSettings)
        {
            SqliteDataManager.BukuKasUmumDataTable table = ((SqliteDataManager)dataset).BukuKasUmum;
            if (table.Count == 0)
            {
                _view.SendMessageInfo("No data to print.");
            }
            _business.PrintExcel(table, printerSettings);
        }

        public void AddNew(IDictionary<string, object> fieldValues)
        {
            fieldValues["bulan"] = (Int32)Enums.GetMonthByName(_view.Bulan);
            fieldValues["tahun"] = Convert.ToInt32(_view.Tahun);
            _business.Add(fieldValues);
        }

        public void DeleteEntry(IList<long> ids)
        {
            _business.Delete(ids);
        }

        public void Edit(IDictionary<string, object> fieldValues)
        {
            _business.Edit(fieldValues);
        }

        public decimal[] HitungJumlah()
        {
            int bulan = (int)Enums.GetMonthByName(_view.Bulan);
            int tahun = Convert.ToInt32(_view.Tahun);
            decimal totalPenerimaan = _business.GetTotalPenerimaan(bulan, tahun);
            decimal totalPengeluaran = _business.GetTotalPengeluaran(bulan, tahun);
            decimal totalPajak = _business.GetTotalPajak(bulan, tahun);

            return new decimal[] { totalPenerimaan, totalPengeluaran, totalPajak };
        }

    }
}
